import request from "../axios";

// 订单相关Api函数

export const createOrder = () => {
  return request({
    url: "/member/order/pre",
    method: "get",
    data: "",
  });
}

/**
 * 查询收货地址 
 * @returns 
 */
export const findAddress = () => {
  return request({
    url: "/member/address",
    method: "get",
    data: '',
  });
}

/**
 * 添加收货地址
 * @param {Object} form 
 * @returns 
 */
export const addAddress = (form) => {
  return request({
    url: "/member/address",
    method: "post",
    data: form,
  });
}

/**
 * 修改收货地址
 * @param {Object} form 
 * @returns 
 */
export const editAddress = (form) => {
  return request({
    url: `/member/address/${form.id}`,
    method: "put",
    data: form,
  });
}


// 删除收货地址 

export const deleteAddress = (id) => {
  return request({
    url: `/member/address/${id}`,
    method: "delete",
    data: { id },
    mock: false
  });
}


/**
 * 提交订单
 * @param {Object} reqParams 
 * @returns 
 */
export const commitOrder = (reqParams) => {
  return request({
    url: `/member/order`,
    method: "post",
    data: reqParams,
  });
}

/**
 * 获取订单详情
 * @param {String} id  订单Id
 * @returns 
 */
export const findOrderDetail = (id) => {
  return request({
    url: `/member/order/${id}`,
    method: "get",
    data: '',
  });
}

/**
 * 查询订单列表
 * @param {Number} orderState - 订单状态，1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消，未传该参数或0为全部
 * @param {Number} page - 页码
 * @param {Number} pageSize - 每页条数
 * @returns
 */
export const findOrderList = ({ orderState, page, pageSize }) => {
  return request({
    url: `/member/order`,
    method: "get",
    data: { orderState, page, pageSize },
  });
}

/**
 * 取消订单
 * @param {String} orderId - 订单ID
 * @param {String} cancelReason - 取消原因
 * @returns Promise
 */
export const cancelOrder = (orderId, cancelReason) => {
  return request({
    url: `/member/order/${orderId}/cancel`,
    method: "put",
    data: { cancelReason },
  });
}

/**
 * 删除订单
 * @param {Array<string>} ids - 删除订单，id集合
 * @returns
 */
export const delteOrder = (ids) => {
  return request({
    url: `/member/order`,
    method: "delete",
    data: { ids: [ids] },
  });
}


/**
 * 确认收货
 * @param {Integer} orderId 订单ID
 * @returns 
 */
export const confOrder = (orderId) => {
  return request({
    url: `/member/order/${orderId}/receipt`,
    method: "put",
  });
}

/**
 * 查看物流
 * @param {String} id - 订单ID
 * @returns
 */
export const logisticsOrder = (id) => {
  return request({
    url: `/member/order/${id}/logistics`,
    method: "get",
  });
}

/**
 * 获取再次购买的订单结算信息
 * @param {String} id - 订单ID
 * @returns
 */
export const findOrderRepurchase = (id) => {
  return request({
    url: `/member/order/repurchase/${id}`,
    method: "get",
  });
}